package com.example.examsystem.controller;

import com.example.examsystem.dto.QuestionDto;
import com.example.examsystem.service.QuestionService;
import com.example.examsystem.common.PageResponse;
import com.example.examsystem.enums.QuestionType;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/questions")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    /**
     * 获取试题列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('question:list:view')")
    public ResponseEntity<PageResponse<QuestionDto>> getQuestions(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String query,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) Long categoryId) {
        
        QuestionType questionType = null;
        if (type != null && !type.isEmpty()) {
            try {
                questionType = QuestionType.valueOf(type);
            } catch (IllegalArgumentException e) {
                log.warn("无效的题型: {}", type);
            }
        }
        
        PageResponse<QuestionDto> questions = questionService.getQuestions(
            page, size, query, questionType, difficulty, subjectId, categoryId);
        return ResponseEntity.ok(questions);
    }

    /**
     * 获取试题详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('question:list:view')")
    public ResponseEntity<QuestionDto> getQuestion(@PathVariable Long id) {
        QuestionDto question = questionService.getQuestionById(id);
        return ResponseEntity.ok(question);
    }

    /**
     * 创建试题
     */
    @PostMapping
    @PreAuthorize("hasAuthority('question:list:create')")
    public ResponseEntity<QuestionDto> createQuestion(@Valid @RequestBody QuestionDto questionDto) {
        QuestionDto created = questionService.createQuestion(questionDto);
        return ResponseEntity.ok(created);
    }

    /**
     * 更新试题
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('question:list:update')")
    public ResponseEntity<QuestionDto> updateQuestion(
            @PathVariable Long id, 
            @Valid @RequestBody QuestionDto questionDto) {
        QuestionDto updated = questionService.updateQuestion(id, questionDto);
        return ResponseEntity.ok(updated);
    }

    /**
     * 删除试题
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('question:list:delete')")
    public ResponseEntity<Void> deleteQuestion(@PathVariable Long id) {
        questionService.deleteQuestion(id);
        return ResponseEntity.ok().build();
    }
}